package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.entity.User;
import com.qk.dto.user.UserDto;

import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/role/{roleLabel}")
    public Result getUserByRole(@PathVariable("roleLabel") String role){
        List<User> list = userService.getUserByRole(role);
        return Result.success(list);
    }


    @GetMapping("/list")
    public Result listAllUsers(){
        List<User> list = userService.listAllUsers();
        return Result.success(list);
    }

    @GetMapping("/dept/{deptId}")
    public Result getUserByDeptId(@PathVariable("deptId") Integer id){
        List<User> list = userService.getUserByDeptId(id);
        return Result.success(list);
    }


    @PutMapping
    public Result updateUser(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }



    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }


    @DeleteMapping("/{ids}")
    public Result deleteUserById(@PathVariable("ids") List<Integer> ids ){
        System.out.println(ids);
        userService.deleteUserById(ids);
        return Result.success();
    }


    @PostMapping
    public Result addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success();
    }


    /**
     * 条件分页查询用户列表
     */
    @GetMapping
    public Result getUsers(UserDto userDto) {
        PageResult<UserDO> userPage = userService.getUsers(userDto);
        return Result.success(userPage);
    }
}